<template>
  <div class="wd-input">
    <input
     class="wd-input_inner"
     :placeholder="placeholder"
     :disabled='disable'
     :class="{isdisable: disable}"
     :name='name'
     :type='(showpassword?(issecrey?"password":"text"):type)'
     :value="value"
     @input="handlevalue"
     >
     <span class="wd-input-suffix" v-if="showsuffix">
       <!-- 需要有clearable属性以及有值才会显示 -->
       <i class="iconfont" v-if="clearable && value" @click="clear">&#xe617;</i>
       <i class="iconfont" v-if="showpassword && value" @click="changetext">&#xe63c;</i>
     </span>
  </div>
</template>

<script>
export default {
  name: 'WdInput',
  data () {
    return {
      issecrey: true
    }
  },
  props: {
    placeholder: {
      type: String,
      default: ''
    },
    disable: {
      type: Boolean,
      default: false
    },
    value: {
      type: String,
      default: ''
    },
    clearable: {
      type: Boolean,
      default: false
    },
    showpassword: {
      type: Boolean,
      default: false
    },
    name: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: 'text'
    }
  },
  methods: {
    handlevalue (e) {
      this.$emit('input', e.target.value)
    },
    clear () {
      this.$emit('input', '')
    },
    changetext () {
      this.issecrey = !this.issecrey
    }
  },
  computed: {
    showsuffix () {
      return this.clearable || this.showpassword
    }
  }
}
</script>

<style scoped lang=scss>
  .wd-input {
    width: 100%;
    position: relative;
    font-size: 14px;
    display: inline-block;
    .wd-input_inner {
      -webkit-appearance: none;
      background-color: #fff;
      background-image: none;
      border-radius: 4px;
      border: 1px solid #dcdfe6;
      box-sizing: border-box;
      color: #606266;
      display: inline-block;
      font-size: inherit;
      height: 40px;
      line-height: 40px;
      outline: none;
      padding: 0 15px;
      transition: border-color .2s cubic-bezier(.645, .45, .355,1);
      width: 100%;
      &:focus {
        outline: none;
        border-color: #409eff;
      }
      &.isdisable {
        background-color: #f5f6fa;
        border-color: #e4e7ed;
        color: #c0c4cc;
        cursor: no-drop;
      }
    }
    .wd-input-suffix {
      position: absolute;
      height: 100%;
      right: 10px;
      top: 0;
      line-height: 40px;
      text-align: center;
      color: #c0c4cc;
      transition: all .3s;
      z-index: 900;
      cursor: pointer;
    }
  }
</style>
